﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace ABM_De_Empleados
{
    class AccesoDatos
    {
        public AccesoDatos()
        {
            cn = new SqlConnection(rutaSQL);
        }

        string rutaSQL = "Data Source=localhost;Initial Catalog=ControlAsistenciaEmpleado;Integrated Security=True"; // ruperto
        SqlConnection cn;
        SqlCommand cmd;

        public void abrir()
        {
            try
            {
                cn.Open();
            }
            catch (SqlException e)
            {
                e.ToString();
            }
        }

        public void cerrar()
        {
            try
            {
                cn.Close();
            }
            catch (SqlException e)
            {
                e.ToString();
            }
        }

        public void ejecutarConsulta(string query)
        {
            this.abrir();
            try
            {
                cmd = new SqlCommand(query, cn);
                cmd.ExecuteNonQuery();
            }
            catch (SqlException e)
            {
                e.ToString();
            }
            this.cerrar();
        }

         public DataTable obtenerDatos (string query)
         {
             DataTable dt = new DataTable();
             this.abrir();
             try
             {
                 SqlDataAdapter da = new SqlDataAdapter(query, cn);
                 da.Fill(dt);
             }
             catch (SqlException e)
             {
                 MessageBox.Show(e.ToString()); 
             }
             this.cerrar();
             return dt;
         }
    }
}
